//
//  HWHomeNormalFieryCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/16.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeNormalFieryCell: UITableViewCell {
    var bgImageView:UIImageView!
    var collectionView:UICollectionView!
    let collectionProtocol = HWHomeNormalFieryCellProtocol()
    var model:HWHomeNormalGameModel?{
        didSet {
            self.collectionProtocol.model = model
            self.collectionView.reloadData()
        }
    }
    var didClickFieryItem:((_:HWHomeGameModel)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWHomeNormalFieryCell) {
        let reUse:String = "HWHomeNormalFieryCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeNormalFieryCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeNormalFieryCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.colorWithHexString(hexString: "FFFFFF")
        
        bgImageView = UIImageView()
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWHomeNormalFieryGameCell.self, forCellWithReuseIdentifier: "HWHomeNormalFieryGameCell")
        contentView.addSubview(collectionView)
    }
    
    func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(112*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWHomeNormalFieryCell {
    func addAction() -> Void {
        self.collectionProtocol.didClickFieryItem = {
            mod in
            self.didClickFieryItem?(mod)
        }
    }
}

class HWHomeNormalFieryCellProtocol: UICollectionViewCell {
    var model:HWHomeNormalGameModel?
    var didClickFieryItem:((_:HWHomeGameModel)->())?
}

extension HWHomeNormalFieryCellProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.didClickFieryItem?(model?.games?[indexPath.row] ?? HWHomeGameModel())
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 13*widthScale, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 13*widthScale
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 168*widthScale, height: 112*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  model?.games?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let gameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeNormalFieryGameCell", for: indexPath) as! HWHomeNormalFieryGameCell
        gameCell.model = model?.games?[indexPath.row]
        return gameCell
    }
}
